﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mogre;

namespace ProjetSMA.View.Mogre
{
    public class Map3D
    {

        private WorldModel m_model;
        private List<Element3D> m_list_elements = new List<Element3D>();
        private Point m_point = new Point(0);
        private List<Entity3D> m_entities = new List<Entity3D>();
        private Root m_root;

        private SceneManager m_scene_mngr;

        public Map3D(SceneManager par_scene, Root par_root)
        {
            m_scene_mngr = par_scene;
            m_root = par_root;

            m_model = new WorldModel();
            m_model.SetUp(30);
            m_model.MyWorld.InitialisationMap();
            m_model.MyWorld.InitialisationNeighboors();
            m_model.MyWorld.InitialisationHeights();
            m_model.MyWorld.InitialisationItemMove();
            m_model.MyWorld.InitialisationFood();
           
            InitializeModel();

        }

        private void InitializeModel()
        {

            for (int i = 0; i < m_model.MyWorld.LenghtMap; i++)
            {
                for (int j = 0; j < m_model.MyWorld.LenghtMap; j++)
                {

                    Element3D loc_element = TestElement(m_model.MyWorld.MapElement[i, j], m_scene_mngr, m_model.MyWorld.MapElement[i, j].Name);
                    loc_element.Position = new Vector3(loc_element.Size * i , 0, loc_element.Size * j);
                    loc_element.HeightItem = m_model.MyWorld.MapElement[i, j].Height;
                    loc_element.Initialisation();

                    m_list_elements.Add(loc_element);

                    if (m_model.MyWorld.MapElement[i, j].Food != null)
                    {
                        Food3D loc_food_3D = new Food3D(m_model.MyWorld.MapElement[i, j], m_scene_mngr, "_food_" + i + "_" + j, m_root);
                        loc_food_3D.Position = new Vector3(loc_food_3D.Size * i, 0, loc_food_3D.Size * j);
                        loc_food_3D.HeightItem = m_model.MyWorld.MapElement[i, j].Height;
                        loc_food_3D.Initialisation();
                    }
                    

                }
              
            }

            for (int i = 0; i < m_model.MyWorld.LengthItemMove; i++)
            {
                
                if (!TestEntity(m_model.MyWorld.ItemsMove[i]))
                {
                    Prey3D d_b = new Prey3D(this, m_model.MyWorld.ItemsMove[i], m_root, m_scene_mngr, "Prey3D_" + i, 20);
                    d_b.Initialisation();
                    m_entities.Add(d_b);
                }
                else
                {
                    Predator3D d_b = new Predator3D(this, m_model.MyWorld.ItemsMove[i], m_root, m_scene_mngr, "Predator3D_" + i, 20);
                    d_b.Initialisation();
                    m_entities.Add(d_b);
                }
               
            }

        }

        public void Killentity(Entity3D loc_ent)
        {

            for (int i = 0; i < ElementsMove.Count; i++)
            {
                if (ElementsMove[i] == loc_ent)
                {
                    ElementsMove.Remove(ElementsMove[i]);
                    ElementsMove[i] = null;
                    break;
                }
            }
        }

        private bool TestEntity(object o)
        {
            if (o is Model.Predator)
                return true;
            return false;
        }

        private bool TestField(object o)
        {
            if (o is Model.Field)
                return true;
            return false;
        }


        private Element3D SearchElement(Model.Element par_item) 
        {
           /* foreach (Element3D item in m_list_elements)
                if (item.ModelElement == par_item)
                    return item;*/
            return null;
        }
         
          private Element3D TestElement(object o, SceneManager par_snmgr, string par_name)
          {
              if (o is Model.Rock)
                  return new View.Mogre.Rock3D((Model.Element)o, par_snmgr,  par_name);
              else
                  return new View.Mogre.Field3D((Model.Element)o, par_snmgr, par_name);
          }


          public List<Entity3D> ElementsMove
          {
              get { return m_entities; }
          }


          public List<Element3D> Elements3D
          {
              get { return m_list_elements; }
          }

          public WorldModel Model
          {
              get { return m_model; }
          }
    }
}
